<?php
header("Content-Type: text/html; charset=iso-8859-1");
class activite{
	var $fields;	
	function activite($id,$field = 'id'){	
		$fields = describe('activites');
		$q = "select * from activites where $field = '$id'";
		$r = ExecRequete($q);
		if(!mysql_num_rows($r)){
#			echo "Aucune activite n'a &eacute;t&eacute; trouv&eacute;e";
			foreach($fields as $field => $type) $this->$field = "";
			$this->champs = array();
		}
		else{
			$s = LigneSuivante($r);
			foreach($fields as $field => $type){
				$this->$field = (get_magic_quotes_gpc()) ? stripslashes($s->$field) : $s->$field;
			}
			$this->fields = describe("activite_".$this->id);
		}
	}
	function resume(){
		echo "<h2>$this->nom</h2>";
		echo "<p class = 'center bold'>".(format_date($this->date))."</p>\n";
		echo prepare_info($this->description);
	}
	function formulaire($id='',$admin = 0){
		if($id){
			$q = "select * from activite_$this->id where id = '$id'";
			$r = ExecRequete($q);
			$s = LigneSuivante($r);
		}
		if(!$admin) echo "<form name = 'myform' id = 'myform' action = 'activites.php'>\n";
		$content = "";
		$hidden = "";
		$disable = ($admin) ? "disabled = 'disabled'" : "";
		$specimen = ($admin) ? "specimen" : "";
		foreach($this->fields as $name => $type){
			$name = str_replace(array(" ","+"),array("_","_et_"),$name);
			if($type != 'int(11)'){
				$content .= leftCol(str_replace("_"," ",$name)." : ",40);
				if($user_id = check_cookie()){
					$membre = new membre($user_id);
					$nom_membre = $membre->prenom." ".$membre->nom;
					$email_membre = $membre->email;
				}
				else {$nom_membre = "";$email_membre = '';}
				if(strpos($type,'varchar') !== FALSE){
					if($name == 'Nom') $value = $nom_membre;
					elseif($name == 'Email') $value = $email_membre;
					else $value = '';
					$value = ($id) ? $s->$name : $value;
					if($admin) $value = $specimen;
					if($this->prive && ($name == 'nom' || $name == 'email')) 
						$col = "<input type = 'hidden' value = '$value' name = '$name' size = '20' />$value";	
					else $col = "<input type = 'text' value = '$value' name = '$name' size = '20' $disabled />";	
				} 
				elseif($type == 'int(1)'){
					if($id){
						$checked = ($s->$name) ? "checked = 'checked'" : "";
					}
					else $checked = "";
					$col = "<input type = 'checkbox' $checked name = '$name' value = '1' $disabled/>";
				}
				elseif($type == 'int(3)') {
					$value = ($id) ? $s->$name : "";
					if($admin) $value = 'xxx';
					$col = "<input type = 'text' name = '$name' value = '$value' size = '2' maxlength ='3' $disabled>";
				}
				else {
					$value = ($id) ? $s->$name : "";
					$col = "<input type = 'hidden' name = '$name' value = '$value' />";
				}
				$content .= rightCol($col,58);
			} 
			else {
				$value = ($name == 'id') ? $id : "";
				$hidden .= "<input type = 'hidden' name = '$name' value = '$value' />";
			}
		}
		$content .= "<br class = 'clear' />\n";
		echo box($this->nom,$content,600,1);
		if(!$admin){
			$hidden .= "<input type = 'hidden' name = 'inscription' value = '$this->id' />";
			echo "<p class = 'center'>$hidden<button type = 'button' onclick = 'inscription_activite()'>Inscrire</button>&nbsp;<button type = 'button' name 'cancel' onclick = 'self.location.href = \"inscriptions.php\"'>annuler</button></p>\n";
			echo "</form>";	
		} 
	}

	function inscriptions($edit){
		echo "<h3>Les inscrits</h3>\n";
		$table = new table();
		$table->filter = 0;
		foreach($this->fields as $name => $type){
			$name = str_replace(array(" ","+"),array("_","_et_"),$name);
			if($type == 'int(11)'){
				$$name = new table_field($name);
				$$name->print = str_replace("_"," ",$name);
				$$name->hidden = 1;
				$table->fields[] = $$name;
			}
			elseif((strpos($type,'varchar') === FALSE) || $name == 'Nom'){ 
				if(strpos($type,"int") !== FALSE) ${"nb_".$name} = 0;
				$$name = new table_field($name);
				$$name->print = str_replace("_"," ",$name);
				if($name != 'nom') $$name->styleclass = 'center';
				$table->fields[] = $$name;
			}			
		}
		if($edit){
			$action = new table_field('actions');
			$action->print = 'actions';
			$action->styleclass = 'center';
			$table->fields[] = $action;
		}
		$q = "select * from activite_$this->id order by nom";
		$r = ExecRequete($q);
		while($s = LigneSuivante($r)){
			$row = new table_row();
			foreach($this->fields as $name => $type){
				$name = str_replace(array(" ","+"),array("_","_et_"),$name);
				if((strpos($type,'varchar') === FALSE) || $name == 'Nom'){ 
					if($type == 'int(1)'){
						${"nb_".$name} += $s->$name;
						$value = ($s->$name) ? "<span class = 'oui'>oui</span>" : "<span class = 'non'>non</span>";
					}
					else {
						if(strpos($type,"int") !== FALSE) ${"nb_".$name} += $s->$name;
						$value = $s->$name;	
					}
					$row->cols[$name] = $value;
				}
			}
			if($edit){
				$value = "<img src = 'images/icon_edit.png' alt = 'edit' height = '14' width = '14' onclick = 'update_inscription_activite($this->id,$s->id)' style = 'cursor: pointer'/><img src = 'images/icon_delete.png' alt = 'del' height = '14' width = '14' onclick = 'if(confirm(\"Voulez-vous vraiment supprimer l inscription ?\" ))delete_inscription_activite($this->id,$s->id)' style = 'cursor: pointer' />";
				$row->cols['actions'] = $value;
			}
			$table->rows[] = $row;
		}
		echo "<div id = 'table_overflow' class = 'overflow' style = 'width: 600px;'>\n";
		$table->styleclass = 'nomargin';
		echo $table->print_header();
		echo utf8_decode($table->print_body());
		echo $table->print_footer();
		echo "</div>\n";
		echo "<p style = 'padding-left: 50px;'>";
		foreach($this->fields as $name => $type){
			if(strpos($type,"int") !== FALSE && $name != 'id'){
				echo "Total ".(str_replace("_"," ",$name))." : ".${"nb_".$name}."<br />";
			}
		}
		echo "</p>";
	}

	function insert(){
		$q = "insert into activites (valide) values(0)";
		$r = ExecRequete($q);
		$this->id = mysql_insert_id();
		// $this->responsable = check_cookie();
		if($this->responsable && $this->description && $this->nom){
			$rand = (ereg("img:(tmp_[0-9]*):",$this->description,$regs)) ? $regs[1] : "";
			$this->description = preg_replace("/img:(tmp_[0-9]*):/","img:$this->id:",$this->description);
			$texte = ($this->valide) ? "activite publi&eacute;e" : "activite mise en attente de publication";
			if($this->update()){
				if($rand){
					rename("images/activites/$rand","images/activites/$this->id");
				}
				$q = "create table activite_$this->id (id integer auto_increment primary key, ";
				foreach($this->champs as $name => $type){
						if($name && $type) $q .= " ".str_replace(array(" ",",",".",";"),array("_","","",""),$name)." $type, ";
				}
				$q = substr($q,0,-2).")";
				ExecRequete($q);
			}  
			return $texte;
		}
		else return "Soit la description, soit le nom est manquant";
	}
	function update(){
		$fields = describe('activites');
		$q = "replace activites set ";
		foreach($fields as $field => $type){
				if($this->$field == 'now()') $q .= $field." = ".$this->$field.", ";
				else $q .= $field." = '".(addslashes($this->$field))."', ";
		}
		if(strstr($q,",") !== false){
			$q = substr($q,0,-2);
			if($r = ExecRequete($q)){
				$this->id = mysql_insert_id();
				return 1;
			}
		}
		else return 0;			
	}
	
	function delete(){
		if(is_dir("images/activites/$this->id")) rmdirtree("images/activites/$this->id");
		if($this->id) $q = "drop table activite_$this->id";
		ExecRequete($q);
		$q = "delete from activites where id = '$this->id' limit 1";
		if(ExecRequete($q)) return 1;
		else return 0;
	}
	
}

?>